<?php
namespace app\index\controller;

use app\common\enums\ResultEnum;
use app\common\model\Agency;
use app\common\model\Brand;
use app\common\model\Company;
use app\common\model\Cars as CarsModel;
use think\Request;
use app\common\model\CarsPark;
use app\common\utils\ResultVOUtil;
use app\common\service\CarsService;
use app\common\model\Terminal;
use app\common\validate\Cars as CarsValidate;
use app\common\exception\ZeduException;
use app\common\model\Personal;

class Cars extends Base
{
    protected $carsService;
    
    public function __construct(CarsService $carsService)
    {
        parent::initialize();
        $this->carsService = $carsService;
    } 
    
    public function index(Request $request){
        $data = $request->param();
       
        $where = [];
        if(!empty($data)){

            if ( !empty($data['search']) ) {
                
                $where[] = ['car_sn|car_unique_id|cardcode|equipment_id','like','%'.$data['search'].'%'];
            }
            
            $agency_id = session('admin.agency_id');
            if(strpos($agency_id, '1')===false){
                
                if ( !empty($data['agency_id']) && strpos($agency_id, $data['agency_id'])) {
                    $where[] = ['agency_id','=',$data['agency_id']];
                }else{
                    $where[] = ['agency_id','in',$agency_id];
                }
            }else{
                if ( !empty($data['agency_id']) ) {
                    $where[] = ['agency_id','=',$data['agency_id']];
                }
            }
            
            if ( !empty($data['brand_id']) ) {
                $where[] = ['brand_id','=',$data['brand_id']];
                
            }
            if ( !empty($data['supplier_no']) ) {
                $where[] = ['supplier_no','=',$data['supplier_no']];
                
            }
            if(!empty($data['car_usage'])){
                if(!empty($data['car_usage'][0])){
                    $where[] = ['car_usage','=',$data['car_usage'][0]];
                }
                if(!empty($data['car_usage'][1])){
                    $where[] = ['relevance','=',$data['car_usage'][1]];
                } 
            }
            
            if ( !empty($data['sim_card']) ) {
                $where[] = ['sim_card','=',$data['sim_card']];
                
            }
            if ( !empty($data['car_park']) ) {
                $where[] = ['car_park','=',$data['car_park']];
                
            }
           
            $result = $this->carsService->index($where, $data['length'], $data['page']);
            
            if($result){
                return ResultVOUtil::success($result);
            }else{
                return ResultVOUtil::error(ResultEnum::ERROR);
            } 
        }
    }
    

	public function list() {
	    $data = [];
	    $data['cars'] = CarsModel::with('brand,agency,carsInfo,carsPark')->order('car_id', 'desc')->limit(10)->select();
	    $data['total'] = CarsModel::count();
	    $data['brand'] = Brand::all(); 
	    $data['company'] = Company::where(['status'=>1])->field('id,name')->select();
	    $data['personal'] = Personal::where(['status'=>1])->field('id,name')->select();
	    $agency_id = session('admin.agency_id');
	    if(strpos($agency_id, '1')===false){
	        $data['agency'] = Agency::where('id','in',$agency_id)->select();    
	    }else{
	        $data['agency'] = Agency::all();
	    }
	    $data['park'] = CarsPark::all();
	    return ResultVOUtil::success($data);
	}

    public function details($id){
        
        $data = $this->carsService->details($id);
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::ERROR);
        }
       
    }
    
    public function bandCars($id){
       
        $data = $this->carsService->bandCars($id);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(['code'=>ResultEnum::UPDATE_ERROR['code'],'msg'=>'绑定失败']);
        }
    }
   
    public function unBandCars($id){
        $data = $this->carsService->unBandCars($id);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(['code'=>ResultEnum::UPDATE_ERROR['code'],'msg'=>'解绑失败']);
        }
    }
    
    public function operate($id,$type){
        $status = $this->carsService->intercept($id, $type);
        if(!$status){
            return ResultVOUtil::error(ResultEnum::OPERATE_ERROR['code'],'请勿频繁操作');
        }
        $data = $this->carsService->operate($id,$type);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(ResultEnum::ERROR);
        } 
    }
    
    public function handleCars($id, $type){
        $status = $this->carsService->intercept($id, 6);
        if(!$status){
            return ResultVOUtil::error(ResultEnum::OPERATE_ERROR['code'],'请勿频繁操作');
        }
        $data = $this->carsService->handleCars($id, $type);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(ResultEnum::ERROR);
        }
    }
    
    public function takePhoto($id) {
        $status = $this->carsService->intercept($id, 7);
        if(!$status){
            return ResultVOUtil::error(ResultEnum::OPERATE_ERROR['code'],'请勿频繁操作');
        }
        $data = $this->carsService->takePhoto($id);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(ResultEnum::ERROR);
        }
    }
    
    public function driverInstall(Request $request) {
        $param = $request->param();
        $validate =  new CarsValidate;
        if(!empty($param['equipment_id'])){
            
            if (!$validate->scene('driver')->check($param)) {
                throw new ZeduException(['code'=>ResultEnum::PARAM_ERROR['code'],'msg'=>$validate->getError()]);
            }
            
        }else{
            if (!$validate->scene('sim')->check($param)) {
                throw new ZeduException(['code'=>ResultEnum::PARAM_ERROR['code'],'msg'=>$validate->getError()]);
            }
        }
        
        $data = $this->carsService->driver($param);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
        }
    }
    
    public function autoTerminal($query =0){
        $where = '%'.$query.'%';
        $data = Terminal::where('equipment_id','like',$where)->limit(10)->select();

        return ResultVOUtil::success($data);

    }
    
    public function tearDown($id){
       $data = $this->carsService->tearDown($id);
       if($data){
           return ResultVOUtil::success('');
       }else{
           return ResultVOUtil::error(ResultEnum::DEL_ERROR);
       }
    }
    
    public function delCars($id){
        $data  = $this->carsService->del($id);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
        }
    }

    public function save(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->carsService->add($param);
            if($data){
                return ResultVOUtil::success('');
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
    public function edit(Request $request) {
        $param = $request->param();
        if(!empty($param)){
            $data = $this->carsService->edit($param);
            if($data){
                return ResultVOUtil::success('');
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
    public function upload(Request $request){
        $param = $request->param();
            
        $data = $this->carsService->uploadFile($param);
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
        }
        
    }
    
    public function getHisInfo($vin=1,$timeStart=0,$timeEnd=0) {
        if($timeStart ==0 || $timeEnd ==0){
            $timeStart = time()-3600;
            $timeEnd = time();
        }else{
            $timeStart = strtotime($timeStart);
            $timeEnd = strtotime($timeEnd);
        }
        
        $data = $this->carsService->history($vin, $timeStart, $timeEnd);
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::FIND_ERROR);
        }
    
    }

}